﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity;
using TestWim.TestWimCL.POCO;

namespace TestWim.TestWimCL.DAL
{
  public class EFDbContextInitializer : DropCreateDatabaseAlways<EFDbContext>
  {
    protected override void Seed(EFDbContext context)
    {
      base.Seed(context);
      Gebruiker geb1 = new Gebruiker { Name = "Gebruiker een", BirthDate = DateTime.Now };
      Nssf nssf1 = new Nssf { Name = "SVS" };
      NssfRepresentation nssfrep1 = new NssfRepresentation { Gebruiker = geb1, Nssf = nssf1, From = DateTime.Now };
      context.Gebruikers.Add(geb1);
      context.Nssfs.Add(nssf1);
      context.NssfRepresentations.Add(nssfrep1);

      Gebruiker geb2 = new Gebruiker { Name = "Gebruiker twee", BirthDate = DateTime.Now };
      Gebruiker geb3 = new Gebruiker { Name = "Gebruiker drie", BirthDate = DateTime.Now };
      Gebruiker geb4 = new Gebruiker { Name = "Gebruiker vier", BirthDate = DateTime.Now };
      Ploeg pl1 = new Ploeg { Name = "Ploeg een" };
      Ploeg pl2 = new Ploeg { Name = "Ploeg twee" };
      PloegMembership pm1 = new PloegMembership { Gebruiker = geb2, Ploeg = pl1, From = DateTime.Now };
      PloegMembership pm2 = new PloegMembership { Gebruiker = geb3, Ploeg = pl2, From = DateTime.Now };
      PloegMembership pm3 = new PloegMembership { Gebruiker = geb4, Ploeg = pl1, From = DateTime.Now };
      PloegMembership pm4 = new PloegMembership { Gebruiker = geb2, Ploeg = pl2, From = DateTime.Now };
      new List<Gebruiker> { geb2, geb3, geb4 }.ForEach(g => context.Gebruikers.Add(g));
      new List<Ploeg> { pl1, pl2 }.ForEach(pl => context.Ploegs.Add(pl));
      new List<PloegMembership> { pm1, pm2, pm3, pm4 }.ForEach(pm => context.PloegMemberships.Add(pm));
      context.SaveChanges();

      Meeting WC2013 = new Meeting { Name = "WC 2013", Location = "Antwerp, Belgium", IsOfficial = true, Date = DateTime.Now, NssfRepresentation=nssfrep1 };
      new List<Meeting>() { WC2013 }.ForEach(m => context.Meetings.Add(m));
      context.SaveChanges();

      Team dse=new Team { Name="De Snelly Eddies" };
      Team dsp=new Team { Name="De Sporters" };
      Team dka=new Team { Name="De Kampioenen" };
      new List<Team>() { dse,dsp,dka }.ForEach(t => context.Teams.Add(t));
      context.SaveChanges();

      Student dse1=new Student { Name="Eddy Janssen" };
      dse1.Teams=new List<Team>();
      dse1.Teams.Add(dse);
      Student dse2=new Student { Name="Eddy Vermeulen" };
      dse2.Teams = new List<Team>();
      dse2.Teams.Add(dse);
      Student dsp1=new Student { Name="Mark Sporter" };
      dsp1.Teams = new List<Team>();
      dsp1.Teams.Add(dsp);
      Student dsp2=new Student { Name="Frank Sporter" };
      dsp2.Teams = new List<Team>();
      dsp2.Teams.Add(dsp);
      Student dka1=new Student { Name="Jan Kampioen" };
      dka1.Teams = new List<Team>();
      dka1.Teams.Add(dka);
      Student dka2=new Student { Name="Jos Kampioen" };
      dka2.Teams = new List<Team>();
      dka2.Teams.Add(dka);
      new List<Student>() { dse1, dse2, dsp1, dsp2, dka1, dka2}.ForEach(s => context.Students.Add(s));
      context.SaveChanges();

      Event ev1 = new Event { Name = "Event 1" };
      Event ev2 = new Event { Name = "Event 2" };
      Event ev3 = new Event { Name = "Event 3" };
      new List<Event>() { ev1, ev2, ev3 }.ForEach(e => context.Events.Add(e));
      context.SaveChanges();

      new List<Result>()
      {
        new Result { ResultValue=5, Meeting=WC2013, Student=dse1, Team=dse, Event=ev1 },
        new Result { ResultValue=4, Meeting=WC2013, Student=dse2, Team=dse, Event=ev1 },
        new Result { ResultValue=6, Meeting=WC2013, Student=dsp1, Team=dsp, Event=ev1 },
        new Result { ResultValue=3, Meeting=WC2013, Student=dsp2, Team=dsp, Event=ev1 },
        new Result { ResultValue=7, Meeting=WC2013, Student=dka1, Team=dka, Event=ev1 },
        new Result { ResultValue=2, Meeting=WC2013, Student=dka2, Team=dka, Event=ev1 },
        new Result { ResultValue=3, Meeting=WC2013, Student=dse1, Team=dse, Event=ev2 },
        new Result { ResultValue=7, Meeting=WC2013, Student=dse2, Team=dse, Event=ev2 },
        new Result { ResultValue=5, Meeting=WC2013, Student=dsp1, Team=dsp, Event=ev2 },
        new Result { ResultValue=6, Meeting=WC2013, Student=dsp2, Team=dsp, Event=ev2 },
        new Result { ResultValue=4, Meeting=WC2013, Student=dka1, Team=dka, Event=ev2 },
        new Result { ResultValue=1, Meeting=WC2013, Student=dka2, Team=dka, Event=ev2 },
        new Result { ResultValue=9, Meeting=WC2013, Student=dse1, Team=dse, Event=ev3 },
        new Result { ResultValue=7, Meeting=WC2013, Student=dse2, Team=dse, Event=ev3 },
        new Result { ResultValue=8, Meeting=WC2013, Student=dsp1, Team=dsp, Event=ev3 },
        new Result { ResultValue=6, Meeting=WC2013, Student=dsp2, Team=dsp, Event=ev3 },
        new Result { ResultValue=5, Meeting=WC2013, Student=dka1, Team=dka, Event=ev3 },
        new Result { ResultValue=3, Meeting=WC2013, Student=dka2, Team=dka, Event=ev3 },
      }.ForEach(r => context.Results.Add(r));
      context.SaveChanges();

      new List<Record>() {
        new Record { ResultValue=1, Meeting=WC2013, Name="Usain Bolt", Event=ev1 },
        new Record { ResultValue=10, Meeting=WC2013, Name="Foo Bar", Event=ev2 },
      }.ForEach(r => context.Records.Add(r));
      context.SaveChanges();

      User us1 = new User { Name = "User Een", BirthDate = DateTime.Now };
      User us2 = new User { Name = "User Twee", BirthDate = DateTime.Now };
      Scc scc1 = new Scc { User = us1 };
      School school1 = new School { Name = "Karel de Grote-Hogeschool", Scc = scc1 };
      context.Users.Add(us1);
      context.Users.Add(us2);
      context.Sccs.Add(scc1);
      context.Schools.Add(school1);
      context.SaveChanges();

      new List<Resultaat> {
        new Resultaat { Event = ev1, PloegMembership = pm1, Meeting = WC2013, ResultaatValue = 5 },
        new Resultaat { Event = ev2, PloegMembership = pm2, Meeting=WC2013, ResultaatValue=3 },
      }.ForEach(r => context.Resultaats.Add(r));
    }
  }
}
